import Taro, { Component } from '@tarojs/taro'
import { connect } from '@tarojs/redux'
import './index.less'
import { View } from '@tarojs/components';
import { AtAvatar, AtButton } from "taro-ui"

@connect(({ personalCenter, loading }) => ({
  personalCenter,
  loading
}))

export default class PersonalCenter extends Component {
  namespace = 'personalCenter'
  config = {
    navigationBarTitleText: '个人中心'
  }

  constructor(props) {
    super(props)
    this.state = {}
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({ type: 'personalCenter/getUserInfo' })
  }

  handleWXGetUserInfo = (event) => {
    const { dispatch } = this.props;
    if (event.detail.errMsg === 'getUserInfo:ok') {

      let userInfo = {
        username: event.detail.userInfo.nickName,
        avatarUrl: event.detail.userInfo.avatarUrl
      }

      dispatch({
        type: 'personalCenter/updateState',
        payload: {
          userInfo: userInfo
        }
      })

      dispatch({ type: 'personalCenter/updateUserInfo' })

    }

  }

  render() {
    const t = this;
    const { dispatch, personalCenter } = this.props;
    const {
      listTab, userInfo
    } = personalCenter;

    const updateState = (obj) => {
      dispatch({
        type: 'personalCenter/updateState',
        payload: {
          ...obj
        }
      })
    }
    return (
      <View className={'personalCenter'}>
        <View className={'nav'}>
          <AtAvatar circle
            image={userInfo.avatarUrl !== undefined && userInfo.avatarUrl !== null ? userInfo.avatarUrl : 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599369136466&di=6d27820e59f54687c1b7eacf7b46d215&imgtype=0&src=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F00%2F94%2F71%2F58dcd84dbf62a_610.jpg'}></AtAvatar>
          <View className={'address'}>
            {userInfo.businessName!==undefined && userInfo.businessName!==null?userInfo.businessName:""}
          </View>
          {userInfo.avatarUrl !== undefined && userInfo.avatarUrl !== null ?
            <View>
              <View className={'name'}>{userInfo.username}</View>
              {/* <View className={'edit'}>修改</View> */}
            </View> :
            <AtButton className={'edit'} openType='getUserInfo' onGetUserInfo={this.handleWXGetUserInfo.bind(this)} >登录</AtButton>}
        </View>

        <View className={'listBox'} key='我的企业' onClick={() => {
          Taro.navigateTo({
            url: '/pages/companyRelease/index'
          })
        }}>
          <View className={'leftBox'}>
            <View className={'tabName'}>我的企业</View>
          </View>
          <View className={'turnRight'}>{'>'}</View>
        </View>

        <View className={'listBox'} key='我的发布' onClick={() => {
        }}>
          <View className={'leftBox'}>
            <View className={'tabName'}>我的发布</View>
          </View>
          <View className={'turnRight'}>{'>'}</View>
        </View>

        <View className={'listBox'} key='我的订阅' onClick={() => {
        }}>
          <View className={'leftBox'}>
            <View className={'tabName'}>我的订阅</View>
          </View>
          <View className={'turnRight'}>{'>'}</View>
        </View>

      </View>
    )
  }
}
